package com.shell.customer.base.service.impl;

import com.shell.bee.base.utils.JsonUtil;
import com.shell.bee.base.utils.StringUtil;
import com.shell.bee.persistence.service.impl.PersistentServiceImpl;
import com.shell.customer.base.dao.CusTagDao;
import com.shell.customer.base.service.CusRecService;
import com.shell.customer.base.service.CusTagRecService;
import com.shell.customer.base.service.CusTagService;
import com.shell.customer.group.service.CusGrpService;
import com.shell.customer.po.CusGrp;
import com.shell.customer.po.CusInvt;
import com.shell.customer.po.CusRec;
import com.shell.customer.po.CusTag;
import com.shell.customer.po.CusTagRec;
import com.shell.feign.PubBizEntFeign;
import com.shell.governor.modules.vo.BizEntVo;
import java.util.ArrayList;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service("cusRecTagService")
public class CusTagRecServiceImpl extends PersistentServiceImpl<CusTagRec> implements CusTagRecService {
	@Autowired
	private CusTagService cusTagService;
	@Autowired
	private CusTagDao cusTagDao;
	@Autowired
	private PubBizEntFeign pubBizEntFeign;
	@Autowired
	private CusGrpService cusGrpService;
	@Autowired
	private CusRecService cusRecService;

	public void saveCusRecTag(String cusId, String grpId, String tagCodes) {
		String[] tagCodeArr = tagCodes.split(",");
		for (String tagCode : tagCodeArr) {
			CusTag cusTag = this.cusTagService.selectCusTagByCode(tagCode);
			String tagId = cusTag.getTagId();
			CusTagRec recTag = new CusTagRec();
			recTag.setCusId(cusId);
			recTag.setGrpId(grpId);
			recTag.setTagId(tagId);
			CusTagRec rt = (CusTagRec) selectOne(recTag);
			if (rt == null) {
				save(recTag);
			}
		}
	}

	public List<String> deleteCusRecTag(String cusId, String grpId, String tagCodes) {
		List<String> delCusIds = new ArrayList();
		String[] tagCodeArr = tagCodes.split(",");
		for (String tagCode : tagCodeArr) {
			CusTag cusTag = this.cusTagService.selectCusTagByCode(tagCode);
			CusTagRec recTag = new CusTagRec();
			if (StringUtil.notEmpty(cusId)) {
				recTag.setCusId(cusId);
			}
			if (StringUtil.notEmpty(grpId)) {
				recTag.setGrpId(grpId);
			}
			recTag.setTagId(cusTag.getTagId());
			List<CusTagRec> tagRecs = selectByEntity(recTag);
			for (CusTagRec tagRec : tagRecs) {
				delCusIds.add(tagRec.getCusId());
				deleteEntity(tagRec);
			}
		}
		return delCusIds;
	}

	public void syncCusBiz(String cusId) {
		String bizIds = this.cusTagDao.selectCusTagBizIds(cusId);
		this.pubBizEntFeign.saveOrUpdateBizEnt(cusId, bizIds);
	}

	public void syncCusBizByTagId(String tagId, String bizIds) {
		List<CusTagRec> tagRecs = selectCusTagRecByTagId(tagId);
		StringBuilder sb = new StringBuilder();
		for (CusTagRec tagRec : tagRecs) {
			sb.append(tagRec.getCusId()).append(",");
		}
		if ((sb != null) && (sb.length() > 0)) {
			syncCusBizBatch(sb.substring(0, sb.length() - 1));
		}
	}

	public List<CusTagRec> selectCusTagRecByTagId(String tagId) {
		CusTagRec tagRec = new CusTagRec();
		tagRec.setTagId(tagId);
		return selectByEntity(tagRec);
	}

	public void syncCusBizBatch(String cusIds) {
		if (StringUtil.empty(cusIds)) {
			return;
		}
		String[] cusIdArr = cusIds.split(",");
		List<BizEntVo> bizEntVos = new ArrayList();
		for (String cusId : cusIdArr) {
			BizEntVo bizEnt = new BizEntVo();
			String bizIds = this.cusTagDao.selectCusTagBizIds(cusId);
			bizEnt.setBizIds(bizIds);
			bizEnt.setEntId(cusId);
			bizEntVos.add(bizEnt);
		}
		try {
			String json = JsonUtil.toJson(bizEntVos);
			this.pubBizEntFeign.saveOrUpdateBizEntBatch(json);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public void addTagByInvt(CusInvt cusInvt) {
		CusGrp grp = (CusGrp) this.cusGrpService.selectByKey(cusInvt.getInviterId());
		String grpId = "";
		if (grp != null) {
			grpId = grp.getGrpId();
		} else {
			CusRec cus = (CusRec) this.cusRecService.selectByKey(cusInvt.getInviterId());
			grpId = cus.getGrpId();
		}
		String tagCode = "MEMBER";
		if (cusInvt.getInviteeCusType().equals("SUPPLIER")) {
			tagCode = "SUPPLIER";
		}
		saveCusRecTag(cusInvt.getInviteeId(), grpId, tagCode);
	}
}
